package com.apartment.common.utils.mp;

import java.util.ArrayList;
import java.util.List;

/**
 * des.
 *
 * @author :  RazaJin
 */
public class SelectBuilder {

    public static final String DOT = ".";
    private List<Property> list;

    public SelectBuilder append(PropertyFunctionEnum functionEnum, String tableName, String propertyName, String propertyAs) {

        propertyAs = propertyAs != null ? propertyAs: propertyName;
        functionEnum = functionEnum == null ? PropertyFunctionEnum.NORMAL : functionEnum;
        //propertyName = tableName != null ? tableName + DOT + propertyName : propertyName;
        if (list == null) {
            list = new ArrayList<>();
        }
        list.add(new Property(tableName, propertyName, propertyAs, functionEnum));
        return this;
    }

    public String toSql() {
        if (list == null || list.size() == 0) {
            return " * ";
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int j = 0; j < list.size() ; j++) {
            Property property = list.get(j);
            if(j != 0){stringBuilder.append(" , ");}
            if(list.get(j).propertyFunctionEnum == PropertyFunctionEnum.ALL){
                stringBuilder.append(property.getTableName())
                        .append(DOT)
                        .append("* ");
            }else{
                stringBuilder.append(list.get(j).propertyFunctionEnum.str)
                        .append("( ")
                        .append(property.getTableName() != null ? property.getTableName() + DOT + property.getPropertyName() : property.getPropertyName())
                        .append(" ) ")
                        .append(" as ")
                        .append(list.get(j).propertyAs);
            }

        }
        return stringBuilder.toString();
    }

    public class Property {
        private String tableName;
        /**
         * 参数名称
         */
        private String propertyName;
        /**
         * 参数值
         */
        private String propertyAs;


        /**
         * 是否是函数
         */
        private PropertyFunctionEnum propertyFunctionEnum;


        public String getTableName() {
            return tableName;
        }

        public void setTableName(String tableName) {
            this.tableName = tableName;
        }

        public String getPropertyName() {
            return propertyName;
        }

        public void setPropertyName(String propertyName) {
            this.propertyName = propertyName;
        }

        public String getPropertyAs() {
            return propertyAs;
        }

        public void setPropertyAs(String propertyAs) {
            this.propertyAs = propertyAs;
        }

        public PropertyFunctionEnum getPropertyFunctionEnum() {
            return propertyFunctionEnum;
        }

        public void setPropertyFunctionEnum(PropertyFunctionEnum propertyFunctionEnum) {
            this.propertyFunctionEnum = propertyFunctionEnum;
        }

        public Property(String tableName, String propertyName, String propertyAs, PropertyFunctionEnum propertyFunctionEnum) {
            this.tableName = tableName;
            this.propertyName = propertyName;
            this.propertyAs = propertyAs;
            this.propertyFunctionEnum = propertyFunctionEnum;
        }

    }

    public enum PropertyFunctionEnum {
        /**  */
        ALL(""),
        /**
         * count
         */
        COUNT("COUNT"),
        /**
         * sum
         */
        SUM("SUM"),
        /**
         * normal
         */
        NORMAL("");
        /**
         *
         */
        private final String str;

        PropertyFunctionEnum(String str) {
            this.str = str;
        }

        public String getStr() {
            return str;
        }
    }

}
